"""
任务相关的Pydantic模式
"""

from typing import Optional, Dict, Any, List
from datetime import datetime
from pydantic import BaseModel, Field


class TaskBase(BaseModel):
    """任务基础模式"""
    name: str = Field(..., min_length=1, max_length=255, description="任务名称")
    description: Optional[str] = Field(None, description="任务描述")
    source_repo_id: int = Field(..., description="源代码库ID")
    target_repos: List[Dict[str, Any]] = Field(..., description="目标代码库列表")
    branch_mapping: Dict[str, Any] = Field(..., description="分支映射配置")
    schedule_config: Optional[Dict[str, Any]] = Field(None, description="定时任务配置")
    files: List[int] = Field(default=[], description="关联文件ID列表")
    force_push: bool = Field(default=False, description="是否强制推送")
    max_concurrent: int = Field(default=3, ge=1, le=10, description="最大并发数")
    retry_count: int = Field(default=3, ge=0, le=10, description="重试次数")
    timeout_seconds: int = Field(default=3600, ge=60, description="超时时间（秒）")
    is_active: bool = Field(default=True, description="是否激活")


class TaskCreate(TaskBase):
    """创建任务模式"""
    pass


class TaskUpdate(BaseModel):
    """更新任务模式"""
    name: Optional[str] = Field(None, min_length=1, max_length=255)
    description: Optional[str] = None
    target_repos: Optional[List[Dict[str, Any]]] = None
    branch_mapping: Optional[Dict[str, Any]] = None
    schedule_config: Optional[Dict[str, Any]] = None
    files: Optional[List[int]] = None
    force_push: Optional[bool] = None
    max_concurrent: Optional[int] = Field(None, ge=1, le=10)
    retry_count: Optional[int] = Field(None, ge=0, le=10)
    timeout_seconds: Optional[int] = Field(None, ge=60)
    is_active: Optional[bool] = None
    status: Optional[str] = None


class TaskResponse(TaskBase):
    """任务响应模式"""
    id: int
    status: str
    last_execution_id: Optional[int]
    created_at: datetime
    updated_at: datetime
    is_scheduled: bool
    target_repo_count: int
    has_files: bool

    class Config:
        from_attributes = True


class TaskListResponse(BaseModel):
    """任务列表响应模式"""
    total: int
    page: int
    size: int
    items: List[TaskResponse]


class TaskExecuteRequest(BaseModel):
    """任务执行请求模式"""
    source_branch: Optional[str] = None
    target_branches: Optional[Dict[int, str]] = None


class TaskExecuteResponse(BaseModel):
    """任务执行响应模式"""
    execution_id: int
    status: str